package com.artqiyi.dollmachine.views.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.model.entity.CoinBuyBean;
import com.artqiyi.dollmachine.views.adapter.CoinBuyAdapter;
import com.artqiyi.dollmachine.views.widget.MyLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Mainly used for confirmation and cancel.
 */
public class RxDialogList extends RxDialog {

    private TextView mTvTitle;
    private RecyclerView recyclerView;
    private ImageView imageClose;

    private CoinBuyAdapter mAdapter;
    private List<CoinBuyBean> mCoinBuyBeans = new ArrayList<>();

    public RxDialogList(Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    public RxDialogList(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView(context);
    }

    public RxDialogList(Context context) {
        super(context);
        initView(context);
    }

    public RxDialogList(Activity context) {
        super(context);
        initView(context);
    }

    public RxDialogList(Context context, float alpha, int gravity) {
        super(context, alpha, gravity);
        initView(context);
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public TextView getTitleView() {
        return mTvTitle;
    }

//    public ImageView getCancelView() {
//        return imageClose;
//    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        imageClose.setOnClickListener(cancelListener);
    }

    public void setData(List<CoinBuyBean> beans) {
        mCoinBuyBeans.clear();
        mCoinBuyBeans.addAll(beans);
        mAdapter.notifyDataSetChanged();
    }

    public void setOnListItemClick(OnClickListener onClickListener ) {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != mCoinBuyBeans && mCoinBuyBeans.size() != 0) {
                    onClickListener.OnListItemClick(mCoinBuyBeans.get(position));
                }
            }
        });
    }

    private void initView(Context context) {
        View dialog_view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_list, null);
        mTvTitle = (TextView) dialog_view.findViewById(R.id.tv_title);
        recyclerView = (RecyclerView) dialog_view.findViewById(R.id.recyclerView);
        imageClose = (ImageView) dialog_view.findViewById(R.id.imageClose);
        mAdapter = new CoinBuyAdapter(mCoinBuyBeans);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
//        recyclerView.addItemDecoration(new RxRecyclerViewDividerTool(ScreenUtil.dp2px(context, 3f)));
        recyclerView.setAdapter(mAdapter);
        setContentView(dialog_view);
    }

    public interface OnClickListener {
        void OnListItemClick(CoinBuyBean bean);
    }
}
